package net.ghsoft.Ctrl
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import spark.components.Group;
	import spark.components.HGroup;
	import spark.components.Image;
	import spark.components.Label;

	public class MyPercent extends HGroup
	{
		public function MyPercent()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, group_creationCompleteHandler);
			super();
		}
		
		private var _lPercent:Label = null;
		
		private var _iPercent:Image = null;
		
		private var _gPercent:Group = null;
		
		protected function group_creationCompleteHandler(event:FlexEvent):void
		{
			verticalAlign = "middle";
			
			_iPercent = new Image();
			_iPercent.percentWidth = 100;
			_iPercent.percentHeight = 100;

			_lPercent = new Label();
			_lPercent.width = 40;
			_lPercent.text = _ShowPercent.toString() + " %";
			addElement(_lPercent);
		
			callLater(SetPercent);
		}
		
		private function SetPercent():void
		{
			if(_gPercent != null)
			{
				removeElement(_gPercent)
			}
			_gPercent = new Group();
			_gPercent.width = height;
			_gPercent.height = height;
			
			DrawSector(_gPercent,_gPercent.width / 2 ,_gPercent.height / 2, _gPercent.height / 2,360 * _ShowPercent / 100,270,0xFA9100,0xFF0000);
			
			addElementAt(_gPercent, 0);
			
//			var bitmapData:BitmapData = new BitmapData(g.width, g.height);
//			bitmapData.draw(g, new Matrix());
//			_iPercent.source = bitmapData;
//			addElement(_iPercent);
			
			_lPercent.text = _ShowPercent.toString() + " %";
		}
		
		private function DrawSector(mc:Group, x:Number=200,y:Number=200,r:Number=100,
									angle:Number=90,startFrom:Number=270,
									color:Number=0x000000, fillColor:Number=0x000000):void
		{
			mc.graphics.lineStyle(0,fillColor);   
			mc.graphics.beginFill(color);
			mc.graphics.moveTo(x,y);
			angle=(Math.abs(angle)>360)?360:angle;
			var n:Number=Math.ceil(Math.abs(angle)/45);
			var angleA:Number=angle/n;
			angleA=angleA*Math.PI/180;
			startFrom=startFrom*Math.PI/180;
			mc.graphics.lineTo(x+r*Math.cos(startFrom),y+r*Math.sin(startFrom));
			for(var i:int = 1;i<=n;i++)
			{
				startFrom+=angleA;
				var angleMid:Number = startFrom-angleA/2;
				var bx:Number = x+r/Math.cos(angleA/2)*Math.cos(angleMid);
				var by:Number = y+r/Math.cos(angleA/2)*Math.sin(angleMid);
				var cx:Number = x+r*Math.cos(startFrom);
				var cy:Number = y+r*Math.sin(startFrom);
				mc.graphics.curveTo(bx,by,cx,cy);
			}
			if(angle!=360)
				mc.graphics.lineTo(x,y);
			mc.graphics.endFill();  // if you want a sector without filling color , please remove this line.
		}
		
		private var _ShowPercent:Number = 0;
		public function set ShowPercent(value:Number):void
		{
			_ShowPercent = value;
			if(_lPercent != null)
			{
				callLater(SetPercent);
			}
		}
		
		public function get ShowPercent():Number
		{
			return _ShowPercent;
		}
	}
}